import json

from django.test import TestCase
from django.utils.six import BytesIO
from rest_framework.parsers import JSONParser

from app.models import FMUser
from app.models.invitation_code import InvitationCode


class RegisterTest(TestCase):
    def setUp(self):
        pass

    def test_register(self):
        data = {
            "username": "15651976282",
            "password": "adminadmin",
            'realname': 'XXX',
            'merchant_name': 'DDDDDD',
            'province': 32,
            'city': 3201,
            'area': 320101,
            'address': 'XXXXXXXXXXXXXXXXX',
            # 'sms_code': '888888',
            'user_type': 1  # 销售
        }
        # response = self.client.post('/api/register/',
        #                             data=json.dumps(data),
        #                             content_type='application/json',
        #                             accept_media_type='application/json')
        # self.assertEqual(response.status_code, 201)
        # json_parser = JSONParser()
        # data = json_parser.parse(BytesIO(response.content))
        # print(data['key'])
        # self.assertTrue(data)

        sales = FMUser.objects.create(**data)
        InvitationCode.objects.create(sales=sales, code='111111')

        data = {
            "username": "15651976283",
            "password": "adminadmin",
            'realname': 'XXX',
            'merchant_name': 'DDDDDD',
            'province': 32,
            'city': 3201,
            'area': 320101,
            'address': 'XXXXXXXXXXXXXXXXX',
            'sms_code': '888888',
            'user_type': 2,
            'code': '111111'
        }
        response = self.client.post('/api/register/',
                                    data=json.dumps(data),
                                    content_type='application/json',
                                    accept_media_type='application/json')
        print(response.content)
        self.assertEquals(response.status_code, 201)

        data = {
            "username": "15651976283",
            "password": "adminadmin",
            'realname': 'XXX',
            'merchant_name': 'DDDDDD',
            'province': 32,
            'city': 3201,
            'area': 320101,
            'address': 'XXXXXXXXXXXXXXXXX',
            'sms_code': '888888',
            'user_type': 2,  # 销售
        }
        response = self.client.post('/api/register/',
                                    data=json.dumps(data),
                                    content_type='application/json',
                                    accept_media_type='application/json')
        self.assertEqual(response.status_code, 400)
